#include "dialogvkb.h"
#include "ui_dialogvkb.h"

QString number[12] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."};

DialogVKB::DialogVKB(QWidget *parent) :
        QDialog(parent, Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint),
        button_group(new QButtonGroup(this)),
        ui(new Ui::DialogVKB)
{
    ui->setupUi(this);
    initGb();
}

DialogVKB::~DialogVKB()
{
    delete ui;
}

void DialogVKB::initGb()
{
    button_group->addButton(ui->pushButton0, 0);
    button_group->addButton(ui->pushButton1, 1);
    button_group->addButton(ui->pushButton2, 2);
    button_group->addButton(ui->pushButton3, 3);
    button_group->addButton(ui->pushButton4, 4);
    button_group->addButton(ui->pushButton5, 5);
    button_group->addButton(ui->pushButton6, 6);
    button_group->addButton(ui->pushButton7, 7);
    button_group->addButton(ui->pushButton8, 8);
    button_group->addButton(ui->pushButton9, 9);
    button_group->addButton(ui->pushButtondot, 10);
    button_group->addButton(ui->pushButtondel, 11);
    button_group->addButton(ui->pushButtonok, 12);

    connect(button_group, SIGNAL(buttonClicked(int)), SLOT(buttonClickResponse(int)));
//    connect(button_group, &QButtonGroup::buttonClicked, &DialogVKB::buttonClickResponse);
}

void DialogVKB::setInputdata(const QString &data)
{
    ui->lineEdit->setText(data);
}

void DialogVKB::buttonClickResponse(int gemfield)
{
    if (gemfield >= 0 && gemfield <= 10)
    {
        event = new QKeyEvent(QEvent::KeyPress, 0, Qt::NoModifier, number[gemfield]);
    }
    else if (gemfield == 11)
    {
        deleteString();
        return;
    }
    else if (gemfield == 12)
    {
        affirmString();
        return;
    }

    ui->lineEdit->setFocus();
    QApplication::sendEvent(focusWidget(), event);
}

void DialogVKB::affirmString()
{
    emit sendMessage(ui->lineEdit->text());               //发送信号给界面
    this->hide();
}

void DialogVKB::deleteString()
{
    event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier);       //新建一个键盘事件
    ui->lineEdit->setFocus();
    QApplication::sendEvent(focusWidget(), event);
}
